// pages/mall/receAddressList/receAddressList.js
import * as Api_Mall_Address from '../../../api/mall/address'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    agree:false,
    addresList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadData()
  },
  useAddr(e){
    let pages = getCurrentPages()
    var prevPage = pages[pages.length - 2];  //上一个页面
    let path = prevPage.route.split("/")
    if(path[path.length-1] == 'index'){
      return
    }
    let addr = e.currentTarget.dataset.addr
    let addrStr = addr.provinceName+" "+addr.cityName +" " +addr.areaName +" "+addr.streetName+" "+addr.addressName
    pages[pages.length - 2].setData({
      addrStr:addrStr,
      addrId:addr.id,
      addr:addr,
      addrFlag:true
    })
    wx.navigateBack({
        delta: 0,
    })
  },
  setDefault(e){
    if(e.currentTarget.dataset.flag == 'N'){
      Api_Mall_Address.setDefault(e.currentTarget.dataset.id).then(res=>{
        this.loadData()
      })
    }else{
      Api_Mall_Address.resetDefault(e.currentTarget.dataset.id).then(res=>{
        this.loadData()
      })
    }
  },
  loadData(){
    Api_Mall_Address.getList().then(res=>{
      this.setData({
        addresList:res.rows
      })
    })
  },
  del(e){
    let that=this;
    wx.showModal({
      title:'删除',
      content:'是否删除此收货地址？',
      success (res) {
        if (res.confirm) {
          Api_Mall_Address.del(e.currentTarget.dataset.id).then(res=>{
            that.loadData()
          })
        }
      }
    })
  
  },
  add(){
    wx.navigateTo({
      url: '../../mall/receAddressEdit/receAddressEdit',
    })
  },
  edit(e){
    console.log(1111)
    wx.navigateTo({
      url: '../../mall/receAddressEdit/receAddressEdit?id='+e.currentTarget.dataset.id,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})